﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Sunboxsoft.WinForm;
using Sunboxsoft.WinForm.Shell.Common;
using System.Diagnostics;

namespace Sunboxsoft.WinForm.Shell
{
    public partial class LaunchForm : Form
    {
        private Thread _thread = null;

        public LaunchForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            _thread = new Thread(Running);
            _thread.Start();

            base.OnLoad(e);
        
        }

        private void Running()
        {
            bool result = Common.ConfigLoader.Load();

            try
            {

                RuntimeData.Instance.ModuleInstanceArray = ModuleManager.Load();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                result = false;
            }
          
            this.BeginInvoke(new Action<bool>(OnLaunchCompleted), result);
        }

        private void OnLaunchCompleted(bool isSuccess)
        {
            if (isSuccess)
            {
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                this.DialogResult = DialogResult.Cancel;
            }
        }
    }
}
